<template>
  <el-dialog :visible.sync="visible" top="20px" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="140px">
      <el-form-item prop="name" :label="$t('arbitration.name')">
        <el-input v-model="dataForm.name" :placeholder="$t('arbitration.name')"/>
      </el-form-item>
        <el-form-item prop="arbitrationFlowId" :label="$t('arbitration.arbitrationFlowName')">
            <el-select
                    v-model="dataForm.arbitrationFlowId"
                    filterable
                    reserve-keyword
                    placeholder="请选择诉讼类型"
                    style="width: 100%"  >
                <el-option
                        v-for="item in arbitrationFlowNameList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
<!--      <el-form-item prop="arbitrationFlowName" :label="$t('arbitration.arbitrationFlowName')">-->
<!--        <el-input v-model="dataForm.arbitrationFlowName" :placeholder="$t('arbitration.arbitrationFlowName')"/>-->
<!--      </el-form-item>-->
      <el-form-item prop="applyPersonName" :label="$t('arbitration.applyPersonName')">
        <el-input v-model="dataForm.applyPersonName" :placeholder="$t('arbitration.applyPersonName')" />
      </el-form-item>
      <el-form-item prop="idNumber" :label="$t('arbitration.idNumber')">
        <el-input v-model="dataForm.idNumber" :placeholder="$t('arbitration.idNumber')" />
      </el-form-item>
      <el-form-item prop="disputeMoney" :label="$t('arbitration.disputeMoney')">
        <el-input-number v-model="dataForm.disputeMoney" :min="0.01" :precision="2" :step="0.1" :placeholder="$t('arbitration.disputeMoney')" />
      </el-form-item>


      <el-form-item prop="remark" :label="$t('remark')">
        <el-input v-model="dataForm.remark" :placeholder="$t('remark')"/>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { debounce } from 'lodash'
  import {regular, validateOrder} from '@/utils/validateHelper'

  export default {
    data () {
      return {
        visible: false,
        arbitrationFlowNameList: [],
        dataForm: {
          id: '',
          name: '',
          arbitrationFlowName: '',
          applyPersonName: '',
          idNumber: '',
          disputeMoney: 0.00,
          qualificationGetDate: '',
          qualificationCode: '',
          arbitrationFlowId: '',
          address: '',
          email: '',
          remark: ''
        },
        loading: false,
      }
    },
    computed: {
      dataRule () {
        return {
          name: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' },
            { min: 2, message: '长度至少2个字符', trigger: 'blur' }
          ],
          qualificationCode: [
            { required: true, message: this.$t('validate.required'), trigger: 'blur' }
          ],
          arbitrationFlowId: [
            { required: true, message: '请选择诉讼流程类型', trigger: 'blur' }
          ],
          applyPersonName: [
            { required: true, message: '请输入申请人姓名', trigger: 'blur' },
            { min: 2, message: '长度至少2个字符', trigger: 'blur' }
          ],
          idNumber: [
            { required: true, message: '请输入身份证号码', trigger: 'blur' },
            {pattern: regular.IDCard, required: true, message: regular.IDCardMsg, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          Promise.all([
              this.getArbitrationFlowIdList()
          ]).then(() => {
            if (this.dataForm.id) {
              this.getInfo()
            }
          })
        })
      },
      // 获取信息
      getInfo () {
        this.$axios.get(`/arbitration/info/${this.dataForm.id}`).then(res => {
          this.dataForm = {
            ...this.dataForm,
            ...res
          }
        }).catch(() => {})
      },
      // 表单提交
      dataFormSubmitHandle: debounce(function () {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            return false
          }
          // this.dataForm.menuIdList = JSON.stringify(this.dataForm.menuIdList)
          this.$http.json('/arbitration/addOrUpdate', this.dataForm).then(res => {
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => {})
          // this.$axios[!this.dataForm.id ? 'post' : 'put']('/role/addRole', JSON.stringify(this.dataForm)).then(res => {
          //   this.$message({
          //     message: this.$t('prompt.success'),
          //     type: 'success',
          //     duration: 500,
          //     onClose: () => {
          //       this.visible = false
          //       this.$emit('refreshDataList')
          //     }
          //   })
          // }).catch(() => {})
        })
      }, 1000, { 'leading': true, 'trailing': false }),

      // 获取诉讼类型列表
      getArbitrationFlowIdList() {
        return this.$axios.get('/arbitrationFlow/page', {
          params: {
            order: 'ASC',
            orderField: 'createTime',
            current: 1,
            size: 20,
            listMode: true,
            queryItems: 'parentId,EQ,0'
          }
        }).then(res => {
          this.loading = false;
          this.arbitrationFlowNameList = res.records
        }).catch(() => {
        })
      }


    }
  }
</script>
